package org.chen.service.impl;

import org.chen.common.model.order.TravelOrder;
import org.chen.common.request.TravelOrderRequest;
import org.chen.common.table.TableItem;
import org.chen.common.vo.OrderTravelInfoVo;
import org.chen.service.AsyncValidationService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@Service
public class AsyncValidationServiceImpl implements AsyncValidationService {

    @Async
    @Override
    public CompletableFuture<OrderTravelInfoVo> validatePreTravelOrderRequest(TravelOrderRequest orderRequest) {
        // 实现验证逻辑
        OrderTravelInfoVo orderInfoVo = new OrderTravelInfoVo();
        TravelOrderRequest order = new TravelOrderRequest();
        order.setName(orderRequest.getName());
        order.setMobile(orderRequest.getMobile());
        order.setEmail(orderRequest.getEmail());
        order.setContent(orderRequest.getContent());
        order.setAgree(orderRequest.isAgree());
        order.setDesc(orderRequest.getDesc());
        List<TravelOrder> orderItems = new ArrayList<>();
        BigDecimal totalAmount = BigDecimal.ZERO;
        int totalCount = 0;
        for (TableItem item : orderRequest.getTabel()) {
            if (item.getId() != null) {  // 跳过最后一个总价项
                TravelOrder orderItem = new TravelOrder();
                orderItem.setProductId(item.getId());
                orderItem.setQuantity(item.getNums());
                orderItem.setType(item.getType());
                orderItem.setPrice(item.getPrice());
                orderItem.setAllPrice(item.getAll_price());
                orderItems.add(orderItem);
                totalAmount = totalAmount.add(item.getAll_price());
                totalCount = totalCount + item.getNums();
            }
        }
        orderInfoVo.setOrder(order);
        orderInfoVo.setTotalCount(totalCount);
        orderInfoVo.setTotalAmount(totalAmount);
        orderInfoVo.setTableItem(orderItems);
        return CompletableFuture.completedFuture(orderInfoVo);
    }
}
